package cn.example.elasticsearch_demo.controller;


import cn.example.elasticsearch_demo.common.CommonPage;
import cn.example.elasticsearch_demo.entiry.ESUser;
import cn.example.elasticsearch_demo.entiry.User;
import cn.example.elasticsearch_demo.service.ESService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags={"ESController"},description ="测试类接口" )
@RestController
@RequestMapping("/es")
public class ESController {

    @ApiOperation(value = "swagger测试类")
    @GetMapping(value = "/test")
    public String test(){
        return "hello";
    }


    @Autowired
    private ESService esService;

    @ApiOperation(value = "保存ES数据")
    @GetMapping("/save")
    public String saveInfo() {

        return esService.saveUser();
    }
    @ApiOperation(value = "删除ES数据")
    @GetMapping("/delete")
    public void deleteInfo() {

         esService.deleteAllUser();
    }
    @ApiOperation(value = "分页查询ES数据")
    @PostMapping("/search")
    public CommonPage<ESUser> searchInfoByKeyWord(String keyword,
                                                  @RequestParam(value = "pageNum", defaultValue = "0") Integer pageNum,
                                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        CommonPage<ESUser> list = esService.searchUser(keyword, pageNum, pageSize);
        return list;
    }
    @ApiOperation(value = "根据id查询数据")
    @PostMapping("/search/{id}")
    public ESUser searchById(@PathVariable Long id) {
        ESUser user = esService.searchById(id);
        return user;
    }


}
